<?php
/**
 * 模型组复制整体妆容使用
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/9/12 16:10
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\Service\Util;


use App\Http\Controllers\ApiController;
use App\Models\Customer\Customer;
use App\Models\Store\Pack;
use App\Services\Repositories\Pack\CopyPackService;
use App\Services\Utils\Transaction;
use Illuminate\Http\Request;

class CopyPackController extends ApiController
{
    /**
     * 批量复制整体妆容
     *
     * @param Request $request
     * @param CopyPackService $copyPackService
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function storeBatch(Request $request,
                               CopyPackService $copyPackService)
    {
        $ids = $request->ids ? explode(',', ($request->ids)) : [];

        $customer = (new Customer)
            ->with('categories')
            ->find($request->customer_id);
            $this->checkItemExist($customer, '客户');

        $packs = (new Pack)
            ->with(['packCategory','assets'])
            ->find($ids);

        $new_packs = Transaction::handle(function () use ($copyPackService, $packs, $customer) {
            return $copyPackService->batchToCustomer($packs, $customer);
        });

        return $this->response(compact('new_packs'));
    }
}